package com.huichenghe.xinlvsh01.expand_activity;


import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatSeekBar;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.text.TextPaint;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.huichenghe.xinlvsh01.R;
import com.huichenghe.xinlvsh01.Utils.MyToastUitls;
import com.lidroid.xutils.BitmapUtils;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class TargetSettingActivity extends AppCompatActivity
{
    private AppCompatSeekBar appCompatSeekBar;
    private AppCompatSeekBar appCompatSeekBarSleep;
    private ArrayList<TextView> aList;
    private ArrayList<TextView> bList;
    private TextView a2000, a4000, a6000, a8000
            , a10000, a12000, a14000, a16000, a18000, a20000
            , sleep1, sleep2, sleep3, sleep4, sleep5;

    private TextView b2000, b4000, b6000, b8000
            , b10000, b12000, b14000, b16000, b18000, b20000
            , sleepb1, sleepb2, sleepb3, sleepb4, sleepb5;
    private TranslateAnimation mTranslateAnimation;
    private ScaleAnimation mScaleAnimation;
    private AnimationSet animSet = new AnimationSet(true);
    private AnimationSet animSetSleep = new AnimationSet(true);
    private TranslateAnimation mSleepTrAnimation;
    private ScaleAnimation mSleepScalAnimation;
    private ArrayList<TextView> tvListSleep;
    boolean isA2000 = false;
    boolean isA4000 = false;
    boolean isA6000 = false;
    boolean isA8000 = false;
    boolean isA10000 = false;
    boolean isA12000 = false;
    boolean isA14000 = false;
    boolean isA16000 = false;
    boolean isA18000 = false;
    boolean isA20000 = false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_target_setting);
        init();
        initText();
//        initAnimationStart();
//        initAnimationStartSleep();
//        initAnimationStop();
        settingTheStatebarAndNavigationbar();
    }


    /**
     * 初始化恢复动画
     */
    private void initAnimationStop()
    {

    }


    /**
     * 初始化动画对象
     */
    private void initAnimationStartSleep()
    {   // 缩放动画
        mSleepScalAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f);
        mSleepScalAnimation.setDuration(200);
        // 移动动画
        mSleepTrAnimation = new TranslateAnimation(0, 0, 0, -72);
        mSleepTrAnimation.setDuration(200);

//        mTranslateAnimation.setRepeatCount(1);
        animSetSleep.addAnimation(mSleepScalAnimation);
        animSetSleep.addAnimation(mSleepTrAnimation);
        animSetSleep.setFillAfter(true);
        animSetSleep.setRepeatCount(1);
        animSetSleep.setInterpolator(this, android.R.anim.accelerate_decelerate_interpolator);

    }

    /**
     * 初始化动画对象
     */
    private void initAnimationStart()
    {   // 缩放动画
        mScaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f);
        mScaleAnimation.setDuration(200);
        // 移动动画
        mTranslateAnimation = new TranslateAnimation(0, -22, 0, -72);
        mTranslateAnimation.setDuration(200);

//        mTranslateAnimation.setRepeatCount(1);
        animSet.addAnimation(mScaleAnimation);
        animSet.addAnimation(mTranslateAnimation);
        animSet.setFillAfter(true);
        animSet.setInterpolator(this, android.R.anim.accelerate_decelerate_interpolator);

    }


    /**
     * 初始化目标TextView
     */
    private void initText()
    {
        // 运动Text
        a2000 = (TextView)findViewById(R.id.a2000);
        a4000 = (TextView)findViewById(R.id.a4000);
        a6000 = (TextView)findViewById(R.id.a6000);
        a8000 = (TextView)findViewById(R.id.a8000);
        a10000 = (TextView)findViewById(R.id.a10000);
        a12000 = (TextView)findViewById(R.id.a12000);
        a14000 = (TextView)findViewById(R.id.a14000);
        a16000 = (TextView)findViewById(R.id.a16000);
        a18000 = (TextView)findViewById(R.id.a180000);
        a20000 = (TextView)findViewById(R.id.a20000);

        b2000 = (TextView)findViewById(R.id.b2000);
        b4000 = (TextView)findViewById(R.id.b4000);
        b6000 = (TextView)findViewById(R.id.b6000);
        b8000 = (TextView)findViewById(R.id.b8000);
        b10000 = (TextView)findViewById(R.id.b10000);
        b12000 = (TextView)findViewById(R.id.b12000);
        b14000 = (TextView)findViewById(R.id.b14000);
        b16000 = (TextView)findViewById(R.id.b16000);
        b18000 = (TextView)findViewById(R.id.b180000);
        b20000 = (TextView)findViewById(R.id.b20000);
        aList.add(a2000);
        aList.add(a4000);
        aList.add(a6000);
        aList.add(a8000);
        aList.add(a10000);
        aList.add(a12000);
        aList.add(a14000);
        aList.add(a16000);
        aList.add(a18000);
        aList.add(a20000);

        bList.add(b2000);
        bList.add(b4000);
        bList.add(b6000);
        bList.add(b8000);
        bList.add(b10000);
        bList.add(b12000);
        bList.add(b14000);
        bList.add(b16000);
        bList.add(b18000);
        bList.add(b20000);
        // 睡眠Text
        sleep1 = (TextView)findViewById(R.id.below_4h);
        sleep2 = (TextView)findViewById(R.id.below_6h);
        sleep3 = (TextView)findViewById(R.id.below_8h);
        sleep4 = (TextView)findViewById(R.id.below_10h);
        sleep5 = (TextView)findViewById(R.id.below_12h);
        tvListSleep.add(sleep1);
        tvListSleep.add(sleep2);
        tvListSleep.add(sleep3);
        tvListSleep.add(sleep4);
        tvListSleep.add(sleep5);

    }



    private void clearOtherAnimSleep(TextView tv)
    {
        for(int j = 0; j < tvListSleep.size(); j ++)
        {
            if(tvListSleep.get(j).getId() != tv.getId())
            {
                TextView v = tvListSleep.get(j);
                v.clearAnimation();
                v.setTextColor(getResources().getColor(R.color.grey_color_dark));

            }
        }
    }

    /**
     * 除当前动画，关闭其他动画
     * @param tv
     */
//    private void clearOtherAnim(TextView tv)
//    {
//        for (int i = 0; i < tvList.size(); i ++)
//        {
//            if(tvList.get(i).getId() != tv.getId())
//            {
//                TextView v = tvList.get(i);
//                v.clearAnimation();
//                v.setTextColor(getResources().getColor(R.color.grey_color_dark));

//                String t = (String)tv.getText();
//                switch (t)
//                {
//                    case "2000":
//                        isA2000 = false;
//                        break;
//                    case "4000":
//                        isA4000 = false;
//                        break;
//                    case "6000":
//                        isA6000 = false;
//                        break;
//                    case "8000":
//                        isA8000 = false;
//                        break;
//                    case "10000":
//                        isA10000 = false;
//                        break;
//                    case "12000":
//                        isA12000 = false;
//                        break;
//                    case "14000":
//                        isA14000 = false;
//                        break;
//                    case "16000":
//                        isA16000 = false;
//                        break;
//                    case "18000":
//                        isA18000 = false;
//                        break;
//                    case "20000":
//                        isA20000 = false;
//                        break;
//                }
//
//            }
//        }
//    }

    /**
     * 设置沉浸式状态栏和底部虚拟键全屏
     */
    private void settingTheStatebarAndNavigationbar() {
        // 透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //底部虚拟键透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }


    // 获取界面对象
    private void init()
    {
        aList = new ArrayList<TextView>();
        bList = new ArrayList<TextView>();
        tvListSleep = new ArrayList<TextView>();
        Toolbar toolbar = (Toolbar)findViewById(R.id.target_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TargetSettingActivity.this.onBackPressed();
            }
        });

        toolbar.setOnMenuItemClickListener(menuListener);

        appCompatSeekBarSleep = (AppCompatSeekBar)findViewById(R.id.sleep_seekBar);
        appCompatSeekBarSleep.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(0 <= progress && progress < 20)
                {
                    clearOtherAnimSleep(sleep1);
                    sleep1.startAnimation(animSetSleep);
                    sleep1.setTextColor(getResources().getColor(R.color.main_background));
                    TextPaint p = sleep1.getPaint();
                    p.setFakeBoldText(true);

                }
                if(20 <= progress && progress < 40)
                {
                    clearOtherAnimSleep(sleep2);
                    sleep2.startAnimation(animSetSleep);
                    sleep2.setTextColor(getResources().getColor(R.color.main_background));
                    TextPaint p = sleep2.getPaint();
                    p.setFakeBoldText(true);
                }

                if(40 <= progress && progress < 60)
                {
                    clearOtherAnimSleep(sleep3);
                    sleep3.startAnimation(animSetSleep);
                    sleep3.setTextColor(getResources().getColor(R.color.main_background));
                    TextPaint p = sleep3.getPaint();
                    p.setFakeBoldText(true);
                }

                if(60 <= progress && progress < 80)
                {
                    clearOtherAnimSleep(sleep4);
                    sleep4.startAnimation(animSetSleep);
                    sleep4.setTextColor(getResources().getColor(R.color.main_background));
                    TextPaint p = sleep4.getPaint();
                    p.setFakeBoldText(true);
                }

                if(80 <= progress && progress < 100)
                {
                    clearOtherAnimSleep(sleep5);
                    sleep5.startAnimation(animSetSleep);
                    sleep5.setTextColor(getResources().getColor(R.color.main_background));
                    TextPaint p = sleep5.getPaint();
                    p.setFakeBoldText(true);
                }



            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                if(0 <= progress &&  progress < 13)
                {
                    seekBar.setProgress(0);
                }
                if(13 <= progress && progress < 38)
                {
                    seekBar.setProgress(25);
                }
                if(38 <= progress && progress < 63)
                {
                    seekBar.setProgress(50);
                }
                if(63 <= progress && progress < 88)
                {
                    seekBar.setProgress(75);
                }
                if(88 <= progress && progress < 100)
                {
                    seekBar.setProgress(100);
                }

            }
        });

        appCompatSeekBar = (AppCompatSeekBar)findViewById(R.id.sport_seekBar);
        appCompatSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
            {
                Log.i("", "当前进度：" + progress);

                if( 0 <= progress && progress < 10)
                {
//                    Log.i("", isA2000 + "+" + isA4000 + isA6000 + "");
//                    if(!isA2000)
//                    {
//                        clearOtherAnim(a2000);
//                        a2000.startAnimation(animSet);
//                        isA2000 = true;
//                        a2000.setTextColor(getResources().getColor(R.color.main_background));
//                        TextPaint p = a2000.getPaint();
//                        p.setFakeBoldText(true);
//                    }
                    showAndHideTheTextView(a2000, b2000, 0);



                }

                if( 10 <= progress && progress < 20)
                {
                    showAndHideTheTextView(a4000, b4000, 1);
//                    a4000.setVisibility(View.INVISIBLE);
//                    b4000.setVisibility(View.VISIBLE);
//                    if(!isA4000)
//                    {
//                        clearOtherAnim(a4000);
//                        a4000.startAnimation(animSet);
//                        isA4000 = true;
//                        a4000.setTextColor(getResources().getColor(R.color.main_background));
//                        TextPaint p = a4000.getPaint();
//                        p.setFakeBoldText(true);
//                    }

                }

                if( 20 <= progress && progress < 30)
                {
                    showAndHideTheTextView(a6000, b6000, 2);
//                    a6000.setVisibility(View.INVISIBLE);
//                    b6000.setVisibility(View.VISIBLE);
//                    if(!isA6000)
//                    {
//                        clearOtherAnim(a6000);
//                        a6000.startAnimation(animSet);
//                        isA6000 = true;
//                        a6000.setTextColor(getResources().getColor(R.color.main_background));
//                        TextPaint p = a6000.getPaint();
//                        p.setFakeBoldText(true);
//                    }

                }

                if( 30 <= progress && progress < 40)
                {
                    showAndHideTheTextView(a8000, b8000, 3);
//                    a8000.setVisibility(View.INVISIBLE);
//                    b8000.setVisibility(View.VISIBLE);
//                    if(!isA8000)
//                    {
//                        clearOtherAnim(a8000);
//                        a8000.startAnimation(animSet);
//                        isA8000 = true;
//                        a8000.setTextColor(getResources().getColor(R.color.main_background));
//                        TextPaint p = a8000.getPaint();
//                        p.setFakeBoldText(true);
//                    }

                }

                if( 40 <= progress && progress < 50)
                {
                    showAndHideTheTextView(a10000, b10000, 4);
//                    a10000.setVisibility(View.INVISIBLE);
//                    b10000.setVisibility(View.VISIBLE);
//                    if(!isA10000)
//                    {
//                        clearOtherAnim(a10000);
//                        a10000.startAnimation(animSet);
//                        isA10000 = true;
//                        a10000.setTextColor(getResources().getColor(R.color.main_background));
//                        TextPaint p = a10000.getPaint();
//                        p.setFakeBoldText(true);
//                    }

                }
                if( 50 <= progress && progress < 60)
                {
                    showAndHideTheTextView(a12000, b12000, 5);
//                    a12000.setVisibility(View.INVISIBLE);
//                    b12000.setVisibility(View.VISIBLE);
//                    if(!isA12000)
//                    {
//                        clearOtherAnim(a12000);
//                        a12000.startAnimation(animSet);
//                        isA12000 = true;
//                        a12000.setTextColor(getResources().getColor(R.color.main_background));
//                        TextPaint p = a12000.getPaint();
//                        p.setFakeBoldText(true);
//                    }


                }
                if(60 <= progress && progress < 70)
                {
                    showAndHideTheTextView(a14000, b14000, 6);
//                    a14000.setVisibility(View.INVISIBLE);
//                    b14000.setVisibility(View.VISIBLE);
//                    if(!isA14000)
//                    {
//                        clearOtherAnim(a14000);
//                        a14000.startAnimation(animSet);
//                        isA14000 = true;
//                        a14000.setTextColor(getResources().getColor(R.color.main_background));
//                        TextPaint p = a14000.getPaint();
//                        p.setFakeBoldText(true);
//                    }

                }
                if(70 <= progress && progress < 80)
                {
                    showAndHideTheTextView(a16000, b16000, 7);
//                    a16000.setVisibility(View.INVISIBLE);
//                    b16000.setVisibility(View.VISIBLE);
//                    if(!isA16000)
//                    {
//                        clearOtherAnim(a16000);
//                        a16000.startAnimation(animSet);
//                        isA16000 = true;
//                        a16000.setTextColor(getResources().getColor(R.color.main_background));
//                        TextPaint p = a16000.getPaint();
//                        p.setFakeBoldText(true);
//                    }

                }

                if(80 <= progress && progress < 90)
                {
                    showAndHideTheTextView(a18000, b18000, 8);
//                    a18000.setVisibility(View.INVISIBLE);
//                    b18000.setVisibility(View.VISIBLE);
//                    if(!isA18000)
//                    {
//                        clearOtherAnim(a18000);
//                        a18000.startAnimation(animSet);
//                        isA18000 = true;
//                        a18000.setTextColor(getResources().getColor(R.color.main_background));
//                        TextPaint p = a18000.getPaint();
//                        p.setFakeBoldText(true);
//                    }

                }

                if(90 <= progress && progress < 100)
                {
                    showAndHideTheTextView(a20000, b20000, 9);
//                    a20000.setVisibility(View.INVISIBLE);
//                    b20000.setVisibility(View.VISIBLE);
//                    if(!isA20000)   // 没有放大显示，则显示
//                    {
//                        clearOtherAnim(a20000);
//                        a20000.startAnimation(animSet);
//                        isA20000 = true;
//                        a20000.setTextColor(getResources().getColor(R.color.main_background));
//                        TextPaint p = a20000.getPaint();
//                        p.setFakeBoldText(true);
//                    }

                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar)
            {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar)
            {
                int progress = seekBar.getProgress();
                if( 0 <= progress && progress < 10)
                {
                    // 在手指松开后定位button的位置
                    seekBar.setProgress(5);
                }

                if( 10 <= progress && progress < 20)
                {
                    seekBar.setProgress(15);
                }

                if( 20 <= progress && progress < 30)
                {
                    seekBar.setProgress(25);

                }

                if( 30 <= progress && progress < 40)
                {
                    seekBar.setProgress(35);

                }

                if( 40 <= progress && progress < 50)
                {
                    seekBar.setProgress(45);

                }
                if( 50 <= progress && progress < 60)
                {
                    seekBar.setProgress(55);


                }
                if(60 <= progress && progress < 70)
                {
                    seekBar.setProgress(65);

                }
                if(70 <= progress && progress < 80)
                {
                    seekBar.setProgress(75);

                }

                if(80 <= progress && progress < 90)
                {
                    seekBar.setProgress(85);

                }

                if(90 <= progress && progress < 100)
                {
                    seekBar.setProgress(95);

                }
            }
        });






//        appCompatSeekBar.setProgressDrawable(getResources().getDrawable(R.drawable.seekbar_bg));
//        appCompatSeekBar.setThumb(getResources().getDrawable(R.drawable.sport_button));
//        appCompatSeekBar.setPadding(10, 30, 10, 30);


    }



    Toolbar.OnMenuItemClickListener menuListener = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId())
            {
                case R.id.target_set_ook:
                    Toast.makeText(TargetSettingActivity.this, "李晓宁", Toast.LENGTH_SHORT).show();
                    break;
            }
            return true;
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_target_ok, menu);
        return true;
    }


    private void showAndHideTheTextView(TextView a2000, TextView b2000, int index)
    {
        if(b2000.getVisibility() == View.INVISIBLE)
        {
            b2000.setVisibility(View.VISIBLE);
            a2000.setVisibility(View.INVISIBLE);
            for (int i = 0; i < bList.size(); i ++)
            {
                if(i != index)
                {
                    TextView b = bList.get(i);
                    if(b.getVisibility() == View.VISIBLE && i != index)
                    {
                        b.setVisibility(View.INVISIBLE);
                    }
                }

            }

            for (int j = 0; j < aList.size(); j ++)
            {
                if(j != index)
                {
                    TextView a = aList.get(j);
                    if(a.getVisibility() == View.INVISIBLE)
                    {
                        a.setVisibility(View.VISIBLE);
                    }


                }
            }
        }
    }
}
